package com.core;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;


public class LocalizableString {

	private static LocalizableString instance;



	public static LocalizableString sharedInstance () {

		if ( instance == null ) {
			instance = new LocalizableString();
		}
		return instance;
	}

	private Map <String, Locale> locales = new HashMap <String, Locale>();



	public LocalizableString() {

	}



	private Locale findOrRecreateLocale ( String localeName ) {

		Locale result;
		if ( localeName == null ) {
			result = Locale.US;
		} else {
			result = locales.get(localeName);
			if ( result == null ) {
				result = new Locale(localeName);
				locales.put(localeName, result);
			}
		}

		return result;
	}



	public String localizable ( String identifier,
	                            String localeName ) {

		Locale locale = this.findOrRecreateLocale(localeName);
		ResourceBundle mainBundle = ResourceBundle.getBundle("Localization", locale);
		return mainBundle.getString(identifier);
	}



	public String localizable ( String identifier ) {

		ResourceBundle mainBundle = ResourceBundle.getBundle("Localization");
		String result = null;
		try {
			result = mainBundle.getString(identifier);
		} catch ( MissingResourceException exception ) {
		}
		return result;
	}
}
